package org.ponly.cta4j.drawing.filter;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 组合滤镜.
 *
 * @author vacoor
 * @since 1.0
 */
public class CompositeFilter implements Filter {
    /**
     * filters.
     */
    private final List<Filter> filters = new ArrayList<>();

    /**
     * 创建一个组合给定滤镜的复合滤镜.
     *
     * @param filters 滤镜
     */
    public CompositeFilter(final Filter... filters) {
        Collections.addAll(this.filters, filters);
    }

    /**
     * 创建一个组合给定滤镜的复合滤镜.
     *
     * @param filters 滤镜
     */
    public CompositeFilter(final List<? extends Filter> filters) {
        this.filters.addAll(filters);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public BufferedImage apply(final BufferedImage image) {
        BufferedImage filtered = image;
        for (final Filter filter : filters) {
            if (null != filter) {
                filtered = filter.apply(filtered);
            }
        }
        return filtered;
    }

    /**
     * 创建一个组合给定滤镜的复合滤镜.
     *
     * @param filters 滤镜
     * @return 复合滤镜
     */
    public static CompositeFilter create(final Filter... filters) {
        return create(Arrays.asList(filters));
    }

    /**
     * 创建一个组合给定滤镜的复合滤镜.
     *
     * @param filters 滤镜
     * @return 复合滤镜
     */
    public static CompositeFilter create(final List<? extends Filter> filters) {
        return new CompositeFilter(filters);
    }
}
